<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Cases;
use Illuminate\Http\Request;

class CaseController extends Controller {


    //列表
    public function index(Request $request){
        $params = [
            'kwd'    => (string)$request->get('kwd',''),
            'status' => (int)$request->get('status',0),
            'cid' => (int)$request->get('cid',0),
        ];
        $lists = Cases::getList($params);
        return $this->writeJson(0,$lists);
    }

    //分类
    public function cate(){
        $cates = Category::where('pid',10)->get()->toArray();
        return $this->writeJson(0,$cates);
    }

    //详情
    public function info(Request $request){
        $id = (int)$request->get('id',0);
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        $info = Cases::findOrFail($id);
        return $this->writeJson(0,$info);
    }

    //更新
    public function update(Request $request)
    {
        $data = $request->all();
        if($data['id']){
            $info = Cases::findOrFail($data['id']);
            $info->update($data);
            return $this->writeJson(0,null,'修改案列信息成功');
        }else{
            unset($data['id']);
            Cases::create($data);
            return $this->writeJson(0,null,'新增案列信息成功');
        }
    }


    //删除
    public function destroy(Request $request){
        $id = (int)$request->get('id',0);
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        Cases::destroy($id);
        return $this->writeJson(0);
    }

}
